﻿--[[
   @brief    ui logic for cloud_game_provider_selector ui
   @author   plane
   @version  1.0
   @date     2020-10-21
--]]

cloud_game_provider_selector_view = {
    tab_exclude_ctrl = nil;
    current_action_info = 0;
    MAX_ITEM_COUNT = 10;
    index = 0;
    canceled = 1;
    item_index_to_channel_id = {};
    already_close = false;

    btn_id_2_info = {};

    kHighlight = "0x646464";
    kHighlightOversea = "0x646464";
    
    init = function() 
        comm_center.reg_service("Svr_CloudGame_ShowProviderInfos", cloud_game_provider_selector_view.set_data);
    end;

    show = function(show, beforeshow)
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if component == nil then
            if show == 1 or beforeshow == 1 then
                base_utility.log("[cloud_game_provider_selector_view]create_dlg.");
                layout_mgr.restore_layout("CloudGameProviderSelector.tpf");
            else
                return;
            end
        end
        component = ui_utility.get_ctrl_ptr_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if component == nil then
            base_utility.log("[cloud_game_provider_selector_view]create_dlg failed.");
            return;
        end
        local id = ui_utility.get_ctrl_id_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if show == 1 then
            --shadow_helper.create_shadow(id);
        end
        cloud_game_provider_selector_view.tab_exclude_ctrl = ui_utility.get_ctrl_ptr_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_EXCLUDER);
        component:Show(show);
        if show == 1 then
            component:BringToTop();
            ui_utility.do_fake_modal(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        end
    end;

    close = function(uilib, dwID_Click)
        base_utility.log("[cloud_game_provider_selector_view]close, index: " .. cloud_game_provider_selector_view.index ..
            ", canceled: " .. cloud_game_provider_selector_view.canceled);
        local game_lib = base_utility.get_component_adapter(
            "IGame_library", "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib == nil then
            return;
        end

        if cloud_game_provider_selector_view.already_close then
            return;
        end;
        cloud_game_provider_selector_view.already_close = true;
        cloud_game_provider_selector_view.tab_exclude_ctrl = nil;
        local param = {};
        param["game_id"] = cloud_game_provider_selector_view.current_action_info;
        param["index"] = cloud_game_provider_selector_view.index;
        param["canceled"] = cloud_game_provider_selector_view.canceled;
       -- comm_center.call_service("Svr_SetCurLoader", param);
        if param["canceled"] == 0 then
            local selected_chanel_id = nil;
            if cloud_game_provider_selector_view.item_index_to_channel_id[param["index"]] ~= nil then
                selected_chanel_id =
                cloud_game_provider_selector_view.item_index_to_channel_id[param["index"]];
            end;

            local selectd_param = {game_id = cloud_game_provider_selector_view.current_action_info,
                channel_id = selected_chanel_id;};
            comm_center.call_service("Svr_CloudGame_SaveSelectedChannel", selectd_param);

            local param = {game_id = cloud_game_provider_selector_view.current_action_info,
                           ignore_time = true,
                           channel_id = selected_chanel_id;};
            comm_center.call_service("SvrLua_CloudGame_Launch_Request", param);
        end;
        cloud_game_provider_selector_view.item_index_to_channel_id = {};
        local id = ui_utility.get_ctrl_id_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if id ~= 0 then
            --shadow_helper.destroy_shadow(id);
            layout_mgr.delay_remove_ctrl(id);
            ui_utility.end_fake_modal(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        end
    end;
    
    set_data = function(action_info)
        cloud_game_provider_selector_view.show(0, 1);
        base_utility.log("[cloud_game_provider_selector_view]".. action_info.game_id);
        cloud_game_provider_selector_view.current_action_info = action_info.game_id;
        cloud_game_provider_selector_view.index = 0;
        cloud_game_provider_selector_view.canceled = 1;
        cloud_game_provider_selector_view.already_close = false;

        local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib == nil then
            return;
        end
        local param = {};
        param["game_id"] = action_info.game_id;
        base_utility.log("[cloud_game_provider_selector_view]set data call Svr_TGPConfigMgr_QueryCloudGameProviderInfo");
        local query_ret = comm_center.call_service("Svr_TGPConfigMgr_QueryCloudGameProviderInfo", param);
        local selector_data = base_utility.json_to_table(query_ret.provider_info_list);
        cloud_game_provider_selector_view.fill_loader_selector(selector_data);
        cloud_game_provider_selector_view.show(1, 0);
    end;

    fill_loader_selector = function (selector_data)
        local data_out = selector_data["providers"];
        base_utility.log("[cloud_game_provider_selector_view]fill_loader_selector: Count~" ..
                selector_data["provider_count"]);
        cloud_game_provider_selector_view.btn_id_2_info = {};
        for i = 1, selector_data["provider_count"] do
                base_utility.log("[cloud_game_provider_selector_view]fill_loader_selector: index~" .. i);
                local item_data = data_out[i];
                --cloud_game_provider_selector_view.process_item_desc(item_data);
                cloud_game_provider_selector_view.set_item(i - 1, 1, item_data);
                if item_data.provider_id ~= nil then
                    cloud_game_provider_selector_view.item_index_to_channel_id[i - 1] =
                    item_data.provider_id;
                end;
            end
        for i = selector_data["provider_count"], cloud_game_provider_selector_view.MAX_ITEM_COUNT do
            base_utility.log("[cloud_game_provider_selector_view]fill_loader_selector: index2~" .. i);
            cloud_game_provider_selector_view.set_item(i, 0, nil);
        end
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if component ~= nil then
            local rc = component:GetRectEx(1,TRUE);
            -- 根据item个数，动态计算窗口高度
            rc.bottom = rc.top + 242 + (selector_data["provider_count"] - 2) * 38;
            component:MoveEx(rc,TRUE);
        end
        local cur_index = 0;
        if selector_data["cur_index"] ~= nil then
            cur_index = selector_data["cur_index"];
            base_utility.log("[cloud_game_provider_selector_view]fill_loader_selector: cur_index~" ..
                selector_data["cur_index"]);
        end
        if cloud_game_provider_selector_view.tab_exclude_ctrl ~= nil then
            cloud_game_provider_selector_view.tab_exclude_ctrl:SetCheckedIndex(cur_index);
        end
    end;

    launch = function()
        if cloud_game_provider_selector_view.tab_exclude_ctrl ~= nil then
            cloud_game_provider_selector_view.index = cloud_game_provider_selector_view.tab_exclude_ctrl:GetCheckedIndex();
            cloud_game_provider_selector_view.canceled = 0;

            base_utility.log("[cloud_game_provider_selector_view]launch loader, index:" ..
                cloud_game_provider_selector_view.index);
        end
        cloud_game_provider_selector_view.close();
    end;

    set_title = function(text)
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_TITLE);
        if component then
            local u16str, str_holder = U8toCUIStr(text);
            component:SetText(u16str);
        end
    end;

    set_desc_item = function(root_id, info)
        if nil == info then return end;
        if Tenio.INVALID_CONTROLID == root_id then return end;
        local cnt_id = ui_utility.get_ctrl_id_by_path(root_id, "CheckBoxCnt");
        if Tenio.INVALID_CONTROLID == cnt_id then return end;
        local desc_id = ui_utility.get_ctrl_id_by_path(cnt_id, "CheckBoxDesc");
        if Tenio.INVALID_CONTROLID == desc_id then return end;

        local available_time = info.available_time or "";
        local text_color = cloud_game_provider_selector_view.kHighlight;
        if base_utility.is_type_of_oversea_ver() then
            text_color = cloud_game_provider_selector_view.kHighlightOversea;
        end

        local cnt_offsetx = 30;
        local cb_text = info.provider_name or "";
        cb_text = "   " .. cb_text;
        local cb_text_width = cloud_game_provider_selector_view.cal_text_width(cb_text);
        cnt_offsetx = cnt_offsetx + cb_text_width;
        ui_utility.setproperty_by_id(root_id, "XOffset", tostring(cnt_offsetx));

        local cx, cy = ui_utility.get_control_size_by_id(root_id);
        if cx ~= 0 then
            cx = cx - cnt_offsetx;
            ui_utility.setproperty_by_id(root_id, "Width", tostring(cx));
        end

        ui_utility.setproperty_by_id(desc_id, "TextColor", text_color);
        ui_utility.setproperty_by_id(desc_id, "TextColorHover", text_color);
        ui_utility.setproperty_by_id(desc_id, "TextColorPressed", text_color);
        ui_utility.setproperty_by_id(desc_id, "TextColorDisable", text_color);

        ui_utility.setproperty_by_id(desc_id, "Caption", available_time);
        ui_utility.show_ctrl(cnt_id, true);

        ui_utility.setproperty_by_id(desc_id, "Disable", "true");
        cloud_game_provider_selector_view.btn_id_2_info[desc_id] = info;
    end;

    set_item = function(index, show, info)
        local u16str, str_holder;
        local main_uilib = ui_utility.uilib();
        local root_id = ui_utility.get_ctrl_id_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        u16str, str_holder = U8toCUIStr("CheckBox_" .. index);
        local item_id = main_uilib:GetControlIDbyPath(u16str, root_id);
        local control = main_uilib:GetControlPtr(item_id);
        if control ~= nil then
            control:Show(show);
            if info ~= nil then
                if info.provider_name ~= nil then
                    ui_utility.setproperty_by_id(item_id, "Caption", "   " .. info.provider_name);
                end;
                if info.provider_ready ~= nil and info.provider_ready == false then
                    ui_utility.setproperty_by_id(item_id, "Disable", "true");
                else
                    ui_utility.setproperty_by_id(item_id, "Disable", "false");
                end;
            end
            if show == 1 then
                local desc_root_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnDescArea");
                cloud_game_provider_selector_view.set_desc_item(desc_root_id, info);
            end
        end
    end;

    cal_text_width = function (str)
        local width = string.len(str);
        local root_id = ui_utility.get_ctrl_id_by_cid(CID_CLOUD_GAME_PROVIDER_SELECTOR_DLG);
        if Tenio.INVALID_CONTROLID == root_id then
            return width;
        end
        local calc_ctrl_id = ui_utility.get_ctrl_id_by_path(root_id, "DummyCtrlForWidthCalc");
        if Tenio.INVALID_CONTROLID == calc_ctrl_id then
            return width;
        end
        ui_utility.setproperty_by_id(calc_ctrl_id, "Caption", str);
        local cal_width = ui_utility.get_control_size_by_id(calc_ctrl_id);
        if 0 ~= cal_witdh  then
            width = cal_width;
        end
        return width;
    end;


    process_item_desc = function(info)
        if nil == info or type(info) ~= 'table' then return end;
        local desc_text = info.loader_desc_text or "";
        local desc_url = info.loader_desc_url or "";
        if "" == desc_text and "" ~= desc_url then
            desc_text = LoadStr("ID_TPF_APP_LOADER_DEFAULT_DESC");
        end
        if desc_text == "" then return end;

        local kMaxCharOneLine = 20;
        -- link desc text
        local desc_cui = U8toCUIStrn(desc_text, 10, 1);
        desc_text = CUIStrtoU8(desc_cui);
        info.loader_desc_text = desc_text;
        local desc_len = base_utility.cal_str_width(desc_text);
        -- loader name
        local name = info.provider_name or "";
        local name_len = base_utility.cal_str_width(name);
        if name_len + desc_len > kMaxCharOneLine then
            desc_cui = U8toCUIStrn(name, kMaxCharOneLine - desc_len, 1);
            info.provider_name = CUIStrtoU8(desc_cui);
        end
    end;
};

cloud_game_provider_selector_view.init();